<?php
    
    include_once "include/constants.php";
    
    $from = $_GET['from'];
    $to = $_GET['to'];

    try {
        $conn = new PDO('mysql:host='.DB_SERVER.';dbname='.DB_NAME, DB_USER, DB_PASS);
        $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
       
         
        $stmt = $conn->prepare("select * from movies2actors where char_num is null limit $from;");
		$stmt->execute();
        
        $m2a = array();
        while ($m2a_row = $stmt->fetch()) {
            $m2a[] = $m2a_row;
        }
        
    } catch(PDOException $e) {
        echo 'ERROR: ' . $e->getMessage();
    }
    
    try {
        $conn = new PDO('mysql:host='.DB_SERVER.';dbname='.DB_NAME, DB_USER, DB_PASS);
        $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
       
        foreach ($m2a as $char) {
            $charname = $char["as_character"];
            $movieid = $char["movieid"];
            $actorid = $char["actorid"];
            
            $regexp = '/.*<(?P<num>\d+)>.*/';
            $num = 0;
            if (preg_match ($regexp, $charname, $matches)) {
                $num = $matches['num'];
            }
            
            $stmt = $conn->prepare("update movies2actors set char_num=$num where movieid=$movieid and actorid=$actorid;");
            $stmt->execute();

        }
        
    } catch(PDOException $e) {
        echo 'ERROR: ' . $e->getMessage();
    }
    
    
    echo "Good!";
    
?>